/*
  * @file JsonFileWriter.cpp
  * @author Thibault Merle et Guillaume Poussel
  */
#include <iostream>
#include <fstream>

#include "base/point_list.h"
#include "io/JsonFileWriter.h"

void JsonFileWriter::write_points(std::ostream& out, const point_list points) const {
    out << "[" << std::endl;
    point_list::const_iterator it = points.begin();
    if (it != points.end()) {
        write_point(out, *it);
    }
    ++it;
    for (; it != points.end(); ++it) {
        out << ',' << std::endl;
        write_point(out, *it);
    }
    out << std::endl << "  ]";
}
void JsonFileWriter::write_point(std::ostream& out, const Point* pt) const {
    out << "    {'x': " << pt->x() << ", 'y': " << pt->y() << '}';
}

void JsonFileWriter::write(const point_list points, const point_list hull) const {
    std::ofstream out(_filename, std::ios::out);
    out << "{" << std::endl;
    out << "  'points': ";
    write_points(out, points);

    out << ',' << std::endl << "  'envelop': ";

    write_points(out, hull);
    out << std::endl << '}';
}


